<?php

namespace Caifenglei\AuthRbac\Http\Requests;

use Caifenglei\AuthRbac\Models\Permission;
use Caifenglei\AuthRbac\Models\Role;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CreateRoleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'max:50', Rule::unique(Role::class, 'name')->ignore($this->id)],
            'label' => ['sometimes', 'max:50', Rule::unique(Role::class, 'label')->ignore($this->id)],
            'sort' => 'sometimes|integer',
            'status' => [
                'required',
                'integer',
                Rule::in([Role::STATUS_AVAILABLE, Role::STATUS_FORBIDDEN])
            ],
            'permissions' => 'sometimes|array',
            'users' => 'sometimes|array',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => '角色标识不能为空',
            'name.max' => '角色标识不能超过50个字符',
            'name.unique' => '角色标识已存在',
            'label.max' => '角色名称不能超过50个字符',
            'label.unique' => '角色名称已存在',
            'sort.integer' => '排序必须为整数',
            'status.required' => '状态不能为空',
        ];
    }
}
